package cn.com.thinker.core.task;

import cn.com.thinker.core.ThinCoreApplication;
import cn.com.thinker.core.config.DruidConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.Future;

/**
 * Created by crazyHC on 2017/6/12.
 */
@SpringBootApplication(scanBasePackages = {"cn.com.thinker.core"})
public class TaskApplication {


    public static void main(String[] args) {
        SpringApplication.run(TaskApplication.class, args);
    }

    @Component
    public class Task {

        public Random random = new Random();

        @Async
        public Future<String> doTaskOne() throws Exception {
            return handle("任务1");
        }

        @Async
        public Future<String> doTaskTwo() throws Exception {
            return handle("任务2");
        }

        @Async
        public Future<String> doTaskThree() throws Exception {
            return handle("任务3");
        }

        private Future<String> handle(String messages) throws InterruptedException {
            System.out.println("开始做" + messages);
            long start = System.currentTimeMillis();
            Thread.sleep(random.nextInt(10000));
            long end = System.currentTimeMillis();
            System.out.println("完成" + messages + "，耗时：" + (end - start) + "毫秒");
            return new AsyncResult<>(messages + "完成");
        }
    }

    @Component
    public class ScheduledTasks {
        private final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        @Scheduled(fixedRate = 5000)
        public void reportCurrentTime() {
            System.out.println("现在时间：" + dateFormat.format(new Date()));
        }
    }
}